package com.mec.web.y2012.coreservlet;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;

public class ServletUtilities {
	
	public static String headWithTitle(String title){
		StringBuilder retval = new StringBuilder();
		
		retval.append("<html><head><title>").append(title)
			.append("</title></head>");
		
		return retval.toString();
	}
	
	
	public static String filter(String input){
		if(!hasSpecialChars(input)){
			return (input);
		}
		
		//
		StringBuffer filtered = new StringBuffer(input.length());
		char c;
		for(int i = 0 ; i < input.length() ; ++i){
			c = input.charAt(i);
			switch(c){
			case '<': filtered.append("&lt;"); break;
			case '>': filtered.append("&gt;"); break;
			case '"': filtered.append("&quote;"); break;
			default: filtered.append(c);
			}
		}
		
		return filtered.toString();
	}
	
	private static boolean hasSpecialChars(String input){
		boolean flag = false;
		if((input != null) && (!input.isEmpty())){
			char c ;
			for(int i = 0 ; i < input.length() ; ++i){
				c = input.charAt(i);
				switch(c){
				case '<':
				case '>':
				case '"':
				case '&':
					flag = true; break;
				}
			}
		}
		return flag;
	}
	
	
	private static final Logger logger = 
			Logger.getLogger(ServletUtilities.class);

}
